package com.foton.logistic.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;

import com.foton.logistic.R;
import com.foton.logistic.adapter.ShopAdapter;
import com.foton.logistic.base.BaseActivity;
import com.foton.logistic.listener.IOnRefreshListener;
import com.foton.logistic.listener.IOnResultListener;
import com.foton.logistic.listener.IOnShopClickListener;
import com.foton.logistic.listener.IOnTryClickListener;
import com.foton.logistic.model.ShopResult;
import com.foton.logistic.task.DispatchTask;
import com.foton.logistic.task.ShowLoadTask;
import com.foton.logistic.task.TaskConstant;
import com.foton.logistic.util.BaseConstant;
import com.foton.logistic.util.OptionUtil;
import com.foton.logistic.util.net.HttpUtil;
import com.foton.logistic.view.ultimate.UltimateRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by wuhaocong on 2017/9/13
 */

public class ShopListActivity extends BaseActivity {
    @InjectView(R.id.ft_fragment_shop_list_rv)
    UltimateRecyclerView ultimateRecyclerView;
    @InjectView(R.id.base_view_contain_layout)
    LinearLayout baseViewContainLayout;
    private LinearLayoutManager linearLayoutManager;
    private ShopAdapter adapter;
    private int pageNumber;
    private ShopResult res;
    private List<ShopResult.DataEntity> resultList = new ArrayList<>();
    public List<ShopResult.DataEntity> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ft_ui_shop);
        ButterKnife.inject(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh(true);
    }

    @Override
    public void init() {
        setTitleAddVisibility(View.VISIBLE);
        setBackLayoutVisibility(View.VISIBLE);
        setTitleText("承运关系管理");
        setTitleTextVisibility(View.VISIBLE);
        initUltimate();

    }

    private void initUltimate() {
        linearLayoutManager = new LinearLayoutManager(this);
        ultimateRecyclerView.setLayoutManager(linearLayoutManager);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        adapter = new ShopAdapter(this, list);
        ultimateRecyclerView.setAdapter(adapter);
        ultimateRecyclerView.enableLoadmore(false);
        ultimateRecyclerView.enableSwipeRefresh(false);
        ultimateRecyclerView.endFinish(false);
        //ultimateRecyclerView.addItemDividerDecoration(this);
        ultimateRecyclerView.setOnRefreshListener(new IOnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh(false);
            }

            @Override
            public void onLoadMore() {
                getRecordList(false);
            }
        });
        adapter.setiOnShopClickListener(new IOnShopClickListener() {
            @Override
            public void onPassClick(int position) {
                String url = BaseConstant.checkCarrier;
                Map<String, Object> argMap = HttpUtil.getEncryMap(ShopListActivity.this);
                argMap.put("Id", list.get(position).Id);
                argMap.put("carrierType", "2");
                ShowLoadTask task = new ShowLoadTask(ShopListActivity.this, taskTag, baseViewContainLayout, loadLayout, "", false, null, url, argMap, TaskConstant.POST);
                //task.setParseClass(ShopResult.class);
                task.setiOnResultListener(new IOnResultListener() {
                    @Override
                    public void onOK(DispatchTask task) {
                        OptionUtil.addToast(ShopListActivity.this, "通过成功");
                        refresh(true);

                    }


                    @Override
                    public void onError(DispatchTask task) {

                    }

                    @Override
                    public void onDone(DispatchTask task) {
                        try {

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                task.execute();
            }




            @Override
            public void onRejectClick(int position) {
            String url = BaseConstant.checkCarrier;
            Map<String, Object> argMap = HttpUtil.getEncryMap(ShopListActivity.this);
            argMap.put("Id", list.get(position).Id);
            argMap.put("carrierType", "3");
                ShowLoadTask task = new ShowLoadTask(ShopListActivity.this, taskTag, baseViewContainLayout, loadLayout, "", false, null, url, argMap, TaskConstant.POST);
           // task.setParseClass(ShopResult.class);
            task.setiOnResultListener(new IOnResultListener() {
                @Override
                public void onOK(DispatchTask task) {
                    OptionUtil.addToast(ShopListActivity.this, "拒绝成功");
                    refresh(true);
                }


                @Override
                public void onError(DispatchTask task) {

                }

                @Override
                public void onDone(DispatchTask task) {
                    try {

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            task.execute();
        }



        });
    }



    void refresh(boolean showLoad) {
        pageNumber = BaseConstant.PAGER_START;
        getRecordList(showLoad);
    }

    IOnTryClickListener iOnTryClickListener = new IOnTryClickListener() {
        @Override
        public void onTry() {
            refresh(true);
        }

        @Override
        public void onOption() {

        }
    };

    void getRecordList(final boolean showLoad) {
        String url = BaseConstant.getCarrierList;
        Map<String, Object> argMap = HttpUtil.getEncryMap(this);
        argMap.put("pageNumber", "" + pageNumber);
        argMap.put("pageSize", "" + BaseConstant.PAGER_COUNT);
        ShowLoadTask task = new ShowLoadTask(this, taskTag, baseViewContainLayout, loadLayout, "", showLoad, iOnTryClickListener, url, argMap, TaskConstant.POST);
        task.setParseClass(ShopResult.class);
        task.setiOnResultListener(new IOnResultListener() {
            @Override
            public void onOK(DispatchTask task) {
                if (task.resultEntity instanceof ShopResult) {
                    res = (ShopResult) task.resultEntity;
                    resultList = res.data;
                }
                updateInfo((ShowLoadTask) task);
            }


            @Override
            public void onError(DispatchTask task) {

            }

            @Override
            public void onDone(DispatchTask task) {
                try {
                    ultimateRecyclerView.refreshFinish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        task.execute();
    }

    void updateInfo(ShowLoadTask task) {


        try {

            if (resultList != null) {
                if (pageNumber == BaseConstant.PAGER_START) {
                    list.clear();
                }
                pageNumber++;
                for (ShopResult.DataEntity dataEntity : resultList) {
                    list.add(dataEntity);
                }
                if (resultList.size() < BaseConstant.PAGER_COUNT) {
                    ultimateRecyclerView.endFinish(true);
                } else {
                    ultimateRecyclerView.endFinish(false);
                }
                if (list.size() == 0 && task != null) {
                    task.addEmptyView("", "", R.mipmap.base_view_link_error);
                }
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


      /*  adapter.setIOnItemClickListener(new IOnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //BroadcastUtil.sendRecommendFotonBroadcast1(RecommendFilterActivity1.this,recommendFilterEntity);
            }
        });
*/
    }

    @OnClick(R.id.base_ui_title_add_layout)
    public void onViewClicked() {
        Intent intent=new Intent(this,AddShopActivity.class);
        startActivity(intent);
    }
}
